package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.SendMe;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.SendMeMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    //注入对象
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;

    @Autowired
    private SendMeMapper sendMeMapper;

    @Autowired
    private IUserService userService;

    @Override
    public PageInfo selectForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyComment> list = strategyCommentMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentMapper.updateByPrimaryKey(strategyComment);
    }

    @Override
    public List<Tag> selectByStrategyId(Long strategyId) {
        return strategyCommentMapper.selectByStrategyId(strategyId);
    }

    @Override
    public List<StrategyComment> selectAllMessage(Long strategyId) {

        List<StrategyComment> strategyComments = strategyCommentMapper.selectAllMessage(strategyId);
        for (StrategyComment strategyComment : strategyComments) {
            String imgUrls = strategyComment.getImgUrls();
            String[] split = imgUrls.split(";");
            strategyComment.setImgUrlss(split);
        }
        return strategyComments;
    }

    @Override
    public List<StrategyComment> selectByCommentId(Long commentId) {


        List<StrategyComment> strategyComments = strategyCommentMapper.selectByCommentId(commentId);
        for (StrategyComment strategyComment : strategyComments) {
            String imgUrls = strategyComment.getImgUrls();
            String[] split = imgUrls.split(";");
            strategyComment.setImgUrlss(split);
        }
        return strategyComments;
    }

    @Override
    public void insert(StrategyComment strategyComment) {
        strategyCommentMapper.insert(strategyComment);

        //新增@用户信息
        //获取输入内容
        String content = strategyComment.getContent();
        User sourceUser = strategyComment.getUser();
        Long strategyCommentId = strategyComment.getId();
        List<User> users = userService.selectAll();
        for (User targetUser : users){
            String nickName = targetUser.getNickName();
            String newName = "@" + nickName;
            if(content.indexOf(newName) != -1){
                SendMe sendMe = new SendMe();
                sendMe.setTargetuser(targetUser);
                sendMe.setType(0);
                sendMeMapper.insert(sendMe);
                Long sendMeId = sendMe.getId();
                sendMeMapper.insertrelation(strategyCommentId, sendMeId);
            }
        }


    }

    @Override
    public List<StrategyComment> queryLsit(Long targetId, int state) {
        return strategyCommentMapper.queryLsit(targetId, state);
    }

}
